/* SchedulerD.java
 * 
 * Alex Schwartz
 * Derek Dupuis
 * Greg Warner
 * Mike Dixey
 * 
 * First level of scheduling, all it tries to do is
 * schedule the tour, if there are any conflicts of
 * capacity then the tour offering is rejected
 * 
 */

import java.util.*;
import java.text.*;

public class SchedulerD implements ScheduleIF
{
   public boolean schedule( TourApp tA, TourOff tO )
   {
      EventColl events = tO.tour().events();
      
      Event e;
      GregorianCalendar start, end;
      
      //must check all events for conflict
      for( int i = 0; i < events.size(); i++ )
      {
         e = events.get( i );
         
         //calculate start and end time of event
         start = (GregorianCalendar)tO.startDate().clone();
         end = (GregorianCalendar)tO.startDate().clone();
         start.add( Calendar.DAY_OF_MONTH, 
                   e.startTime().get( Calendar.DAY_OF_MONTH ) - 1 );
         start.add( Calendar.HOUR_OF_DAY,
                   e.startTime().get( Calendar.HOUR_OF_DAY ) );
         start.add( Calendar.MINUTE,
                   e.startTime().get( Calendar.MINUTE ) );
         
         end.add( Calendar.DAY_OF_MONTH, 
                 e.endTime().get( Calendar.DAY_OF_MONTH ) - 1 );
         end.add( Calendar.HOUR_OF_DAY,
                 e.endTime().get( Calendar.HOUR_OF_DAY ) );
         end.add( Calendar.MINUTE,
                 e.endTime().get( Calendar.MINUTE ) );
         //check if event works with provider
         Provider p = e.provider();
         if( p.service().type().compareTo( "lodging" ) == 0 )
         {
            if( p.conflict( tO.roomsNeeded(), start, end ) )
               return false;
         }
         else if( p.conflict( tO.size(), start, end ) )
            return false;
      }
      
      //if it gets here, no conflict detected schedule all events
      for( int i = 0; i < events.size(); i++ )
      {
         e = events.get( i );
         
         //calculate start and end time of event
         start = (GregorianCalendar)tO.startDate().clone();
         end = (GregorianCalendar)tO.startDate().clone();
         start.add( Calendar.DAY_OF_MONTH, 
                   e.startTime().get( Calendar.DAY_OF_MONTH ) - 1 );
         start.add( Calendar.HOUR_OF_DAY,
                   e.startTime().get( Calendar.HOUR_OF_DAY ) );
         start.add( Calendar.MINUTE,
                   e.startTime().get( Calendar.MINUTE ) );
         
         end.add( Calendar.DAY_OF_MONTH, 
                 e.endTime().get( Calendar.DAY_OF_MONTH ) - 1 );
         end.add( Calendar.HOUR_OF_DAY,
                 e.endTime().get( Calendar.HOUR_OF_DAY ) );
         end.add( Calendar.MINUTE,
                 e.endTime().get( Calendar.MINUTE ) );
         
         //schedule event
         Provider p = e.provider();
         if( p.service().type().compareTo( "lodging" ) == 0 )
            p.addBooking( new Booking( tO.roomsNeeded()
                                         , start, end ) );
         else 
            p.addBooking( new Booking( tO.size(),
                                      start, end ) );
         
      }
      System.out.printf( "Offering: %s was scheduled normally.", tO.offeringName() );
      return true;
   }
}